import {
  setUserInfo,
  getUserInfo,
  setUserToken,
  getUserToken,
  getUserDetailInfo,
  setUserDetailInfo
} from '../../common/storage.js'
export default {
  namespaced: true,
  state: {
    // 用户的地址
    address: JSON.parse(getUserInfo() || '{}'),
    token: getUserToken() || '',
    userInfo: JSON.parse(getUserDetailInfo() || '{}'),
    // 重定向页面跳转的数据
    redirectInfo: null
  },
  mutations: {
    // 修改用户的地址
    updateUserAddr(state, address) {
      state.address['name'] = address.userName
      state.address['tel'] = address.telNumber
      state.address['addr'] = address.fullAddr
      state.address['detail'] = address.detailInfo
      // 本地持久化
      setUserInfo(state.address)
    },
    // 修改用户token
    updateToken(state, token) {
      state.token = token
      // token 本地持久化存储
      setUserToken(token)
    },
    // 更新通过授权获得的用户资料信息
    updateUserInfo(state, userInfo) {
      state.userInfo = userInfo
      // 将用户信息存到本地
      setUserDetailInfo(state.userInfo)
    },
    // 更新重定向的相关页面数据信息
    updateRedirect(state,redirect){
      state.redirectInfo = redirect
    }
  },
  actions: {}
}
